<?php

App::uses('AppController', 'Controller');

/**
 * ProductShops Controller
 *
 * @property ProductShop $ProductShop
 */
class ProductShopsController extends AppController {

  /**
   * index method
   *
   * @return void
   */
  public function index() {
    $this->ProductShop->recursive = 0;
//    $this->paginate = array('conditions' => array('ProductShop.shop_id' => $this->Session->read('Auth.User.shop_id') ) );
    $this->set('productShops', $this->paginate());
  }

  /**
   * view method
   *
   * @throws NotFoundException
   * @param string $id
   * @return void
   */
  public function view($id = null) {
    if (!$this->ProductShop->exists($id)) {
      throw new NotFoundException(__('Invalid product shop'));
    }
    $options = array('conditions' => array('ProductShop.' . $this->ProductShop->primaryKey => $id));
    $this->set('productShop', $this->ProductShop->find('first', $options));
  }

  /**
   * add method
   *
   * @return void
   */
  public function add() {
    if ($this->request->is('post')) {
      $this->ProductShop->create();
      if ($this->ProductShop->save($this->request->data)) {
        $this->Session->setFlash(__('The product shop has been saved'));
        $this->redirect(array('action' => 'index'));
      } else {
        $this->Session->setFlash(__('The product shop could not be saved. Please, try again.'));
      }
    }
    $shops = $this->ProductShop->Shop->find('list');
    $products = $this->ProductShop->Product->find('list');
    $this->set(compact('shops', 'products'));
  }

  /**
   * updateStock method
   *
   * @return void
   */
  public function updateStock() {

    if ($this->request->is('post')) {

      $shop_id = $this->request->data['ProductShop']['shop_id'];
      $product_id = $this->request->data['ProductShop']['product_id'];
      $quantity = $this->request->data['ProductShop']['quantity'];
      $purchase_price = $this->request->data['ProductShop']['purchase_price'];
      $date = $this->request->data['ProductShop']['date'];


      $hproduct = $this->ProductShop->find('first', array('fields' => array(
              'ProductShop.id, ProductShop.current_stock'), 'conditions' => array('ProductShop.shop_id' => $shop_id,
              'ProductShop.product_id' => $product_id)));


      $this->ProductShop->id = $hproduct['ProductShop']['id'];

      $update_stock = ($hproduct['ProductShop']['current_stock'] + $quantity);
      if ($this->ProductShop->saveField('current_stock', $update_stock)) {

        if ($this->addHProduct($shop_id, $product_id, $purchase_price, $quantity, $date)) {

          $this->Session->setFlash(__('Producto Actualizado'));
          $this->redirect(array('action' => 'index'));
        }
      }
    }
    $shops = $this->ProductShop->Shop->find('list');
    $products = $this->ProductShop->Product->find('list');
    $this->set(compact('shops', 'products'));
  }

  /**
   * edit method
   *
   * @throws NotFoundException
   * @param string $id
   * @return void
   */
  public function edit($id = null) {
    if (!$this->ProductShop->exists($id)) {
      throw new NotFoundException(__('Invalid product shop'));
    }
    if ($this->request->is('post') || $this->request->is('put')) {
      if ($this->ProductShop->save($this->request->data)) {
        $this->Session->setFlash(__('The product shop has been saved'));
        $this->redirect(array('action' => 'index'));
      } else {
        $this->Session->setFlash(__('The product shop could not be saved. Please, try again.'));
      }
    } else {
      $options = array('conditions' => array('ProductShop.' . $this->ProductShop->primaryKey => $id));
      $this->request->data = $this->ProductShop->find('first', $options);
    }
    $shops = $this->ProductShop->Shop->find('list');
    $products = $this->ProductShop->Product->find('list');
    $this->set(compact('shops', 'products'));
  }

  /**
   * delete method
   *
   * @throws NotFoundException
   * @throws MethodNotAllowedException
   * @param string $id
   * @return void
   */
  public function delete($id = null) {
    $this->ProductShop->id = $id;
    if (!$this->ProductShop->exists()) {
      throw new NotFoundException(__('Invalid product shop'));
    }
    $this->request->onlyAllow('post', 'delete');
    if ($this->ProductShop->delete()) {
      $this->Session->setFlash(__('Product shop deleted'));
      $this->redirect(array('action' => 'index'));
    }
    $this->Session->setFlash(__('Product shop was not deleted'));
    $this->redirect(array('action' => 'index'));
  }

  public function addHProduct($shop_id = null, $product_id = null, $purchase_price = null, $quantity = null, $date = null) {
    //    Guardo Historico de Compras
    $data = array(
        'HProduct' => array('shop_id' => $shop_id, 'product_id' => $product_id,
            'purchase_price' => $purchase_price, 'quantity' => $quantity,
            'date' => $date)
    );
    $this->loadModel('HProduct');
    $this->HProduct->create();
    if ($this->HProduct->save($data)) {
      return true;
    }
  }

}
